<?php

class DiscuzController extends ApiController {

	/**
	 * 输出 json 范例
	 * URL: /WitSdk-Dev/www/index.php/plugin/Sample/v1/discuz
	 * 使用前提：在 Discuz! 应用中心安装了 WitFrame API 插件的站点，且项目的系统类型为 ”For Discuz!X“
	 */
	public function indexAction() {

		echo "\nInit";
		$v = Lib\Site::Client_Page(
			array(
				'script' => 'member',
				'output' => array(
					'_G/uid,username,groupid,timestamp' => 'm'
				),
				'get' => array(
					'mod' => 'logging',
					'action' => 'login',
				),
			)
		);
		$session = $v['_session'];
		$formHash = $v['_formhash'];
		print_r($v);

		echo "\nLogin";
		$v = Lib\Site::Client_Page(
			array(
				'script' => 'member',
				'output' => array(
					'_G/uid,username,groupid,timestamp' => 'm',
					'_G/messageparam' => 'msg',
					'_attachhash' => true,
				),
				'get' => array(
					'mod' => 'logging', 'action' => 'login', 'loginsubmit' => 'yes', 'lssubmit' => 'yes',
				),
				'post' => array('fastloginfield' => 'username', 'username' => 'admin', 'password' => 1, 'formhash' => $formHash),
				'session' => $session,
			)
		);
		$session = $v['_session'];
		$formHash = $v['_formhash'];
		$hash = $v['_attachhash'];
		print_r($v);

		echo "\nGetSession";
		$v = Lib\Site::Client_Page(['cmd' => 'getSession', 'session' => $session]);
		$sessionData = $v['_result'];
		print_r($v);

		echo "\nNewThread";
		$v = Lib\Site::Client_Page(
			array(
				'script' => 'forum',
				'output' => array(
					'_G/uid,username,groupid,timestamp' => 'm',
					'_G/messageparam' => 'msg',
				),
				'get' => array(
					'mod' => 'post', 'action' => 'newthread', 'topicsubmit' => 'yes', 'fid' => 2,
				),
				'post' => array('subject' => '标题' . time(), 'message' => '内容' . time(), 'formhash' => $formHash),
				'sessionData' => $sessionData,
			)
		);
		print_r($v);

		echo "\nUpload";
		$files = array();
		if (!empty($_FILES['file']) && $_FILES['file']['error'] == 0) {
			$files['Filedata'] = $_FILES['file'];
		}

		$v = Lib\Site::Client_Page(
			array(
				'script' => 'misc',
				'get' => array(
					'mod' => 'swfupload', 'action' => 'swfupload', 'operation' => 'upload', 'fid' => 2,
				),
				'post' => array(
					'uid' => 1, 'hash' => $hash,
				),
				'file' => $files,
				'session' => $session,
			),
		);
		print_r($v);

		if (Lib\Site::Client_Type() == 'Discuz') {
			//登录
			$info['loginStatus'] = Lib\Site::Client_CheckLogin([
				'username' => 'admin',
				'password' => '1',
			]);
			//获取用户信息
			$info['userinfo'] = Lib\Site::Client_GetUserInfo([
				'username' => 'admin',
			]);
			//获取站点信息
			$info['siteinfo'] = Lib\Site::Client_GetSiteInfo();
		}
		print_r($info);
		exit;

		$v = Lib\Site::Client_Page(
			array(
				'script' => 'misc',
				'rawOutput' => true,
				'get' => array(
					'mod' => 'seccode',
					'idHash' => 'xxx',
				),
			)
		);
		header('Content-type: image/png');
		print_r(base64_decode($v));exit;

		$this->successMessage('', array('info' => $info));
	}


}
